package com.myapp.ruyixing.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ShopAlbum.PhotoItem;
import com.myapp.ruyixing.model.ShopAlbum.ShopSubAlbum;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;

public class ShopSubAlbumActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {

	private GridView mGridView;
	private TextView mEmptyView;

	private String mShopId;
	private String mShopName;
	private String mCurrTypeId;

	private RYHttpResponseHandler mTypeHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ShopSubAlbum data = (ShopSubAlbum) o;

			if (data != null && data.items != null) {
				MyListAdapter adapter = new MyListAdapter(data.items);
				mGridView.setAdapter(adapter);
			}
			mGridView.setEmptyView(mEmptyView);
			((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_sub_album);

		initView();

		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);

		mShopId = getIntent().getStringExtra(ShopDetailActivity.INTENT_SHOP_ID);
		mShopName = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_NAME);
		String title = getIntent().getStringExtra("title");
		mCurrTypeId = getIntent().getStringExtra("typeid");
		if (TextUtils.isEmpty(mShopId) == true) {
			Toast.makeText(getApplicationContext(), "商户为空", Toast.LENGTH_SHORT)
					.show();
		} else {
			RYHttpClient.getShopTypeAlbum(this, mTypeHandler, mShopId,
					mCurrTypeId);
		}

		if (TextUtils.isEmpty(mShopName) == false
				&& TextUtils.isEmpty(title) == false) {
			((TitleBar) findViewById(R.id.title_bar)).setTitleText(title + "\n"
					+ mShopName);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		PhotoItem item = (PhotoItem) mGridView.getAdapter().getItem(position);

		startActivity(new Intent(this, ShopBigAlbumActivity.class)
				.putExtra(ShopDetailActivity.INTENT_SHOP_ID, mShopId)
				.putExtra(ShopDetailActivity.INTENT_SHOP_NAME, mShopName)
				.putExtra("title", item.title)
				.putExtra("typeid", mCurrTypeId)
				.putExtra("position", position)
				.putExtra("photos",
						((MyListAdapter) mGridView.getAdapter()).mData));
	}

	private void initView() {
		mGridView = (GridView) findViewById(R.id.gridview);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mGridView.setOnItemClickListener(this);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
	}

	private class ViewHolder {
		TextView mNameView;
		TextView mPriceView;
		NetworkImageView mShopImage;
		TextView mRecommendView;
	}

	private class MyListAdapter extends ArrayAdapter<PhotoItem> {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;
		ArrayList<PhotoItem> mData;

		public MyListAdapter(List<PhotoItem> list) {
			super(ShopSubAlbumActivity.this, -1, list);
			mData = (ArrayList<PhotoItem>) list;
			mQueue = Volley.newRequestQueue(ShopSubAlbumActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_shop_album, parent, false);

				ViewHolder holder = new ViewHolder();
				holder.mNameView = (TextView) convertView
						.findViewById(R.id.text_name);
				holder.mPriceView = (TextView) convertView
						.findViewById(R.id.text_price);
				holder.mRecommendView = (TextView) convertView
						.findViewById(R.id.text_recommend);
				holder.mShopImage = (NetworkImageView) convertView
						.findViewById(R.id.picture);

				convertView.setTag(holder);
			}

			ViewHolder holder = (ViewHolder) convertView.getTag();
			PhotoItem item = getItem(position);

			holder.mNameView.setText(item.title);
			holder.mPriceView.setText(item.price);
			holder.mRecommendView.setText(item.score + "人推荐");
			if (!TextUtils.isEmpty(item.picurl)) {
				holder.mShopImage.setImageUrl(item.picurl, mImageLoader);
			}

			return convertView;

		}
	}
}
